﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BOSS.Framework;

namespace BOSS.Domain.Security
{
    /// <summary>
    /// 帐号
    /// </summary>
    [Serializable]
    public class Account : DomainEntity<int>, IPrivilegeContainer
    {
        /// <summary>
        /// 获取 / 设置 登录名
        /// </summary>
        public virtual string LoginName { get; set; }

        /// <summary>
        /// 获取 / 设置 登录密码
        /// </summary>
        public virtual string Password { get; set; }

        /// <summary>
        /// 获取 / 设置 工号
        /// </summary>
        public virtual string EmployeeId { get; set; }

        /// <summary>
        /// 获取 / 设置 姓名
        /// </summary>
        public virtual string FullName { get; set; }

        /// <summary>
        /// 获取 / 设置 身份证号
        /// </summary>
        public virtual string IdentityCard { get; set; }

        /// <summary>
        /// 获取 / 设置 电话
        /// </summary>
        public virtual string Telephone { get; set; }

        /// <summary>
        /// 获取 / 设置 手机
        /// </summary>
        public virtual string Mobile { get; set; }

        /// <summary>
        /// 获取 / 设置 邮箱
        /// </summary>
        public virtual string Email { get; set; }

        /// <summary>
        /// 获取 / 设置 QQ
        /// </summary>
        public virtual string QQ { get; set; }

        /// <summary>
        /// 获取 / 设置 MSN
        /// </summary>
        public virtual string MSN { get; set; }

        /// <summary>
        /// 获取 / 设置 是否已禁用
        /// </summary>
        public virtual bool Disabled { get; set; }

        /// <summary>
        /// 获取 / 设置 入职日期
        /// </summary>
        public virtual DateTime StartingDate { get; set; }

        /// <summary>
        /// 获取 / 设置 离职日期
        /// </summary>
        public virtual DateTime? LeaveDate { get; set; }

        /// <summary>
        /// 获取 / 设置 角色
        /// </summary>
        public virtual IList<Role> Roles { get; set; }

        /// <summary>
        /// 添加一个新角色（会自动在 Role 中建立相应关联）
        /// </summary>
        /// <param name="role"></param>
        public virtual void AddRole(Role role)
        {
            InternalAddRole(role);
            role.InternalAddAccount(this);
        }

        /// <summary>
        /// 移除一个新角色（会自动在 Role 中删除相应关联）
        /// </summary>
        /// <param name="role"></param>
        public virtual void RemoveRole(Role role)
        {
            InternalRemoveRole(role);
            role.InternalRemoveAccount(this);
        }


        /// <summary>
        /// 添加一个新角色
        /// </summary>
        /// <param name="role"></param>
        protected internal virtual void InternalAddRole(Role role)
        {
            Check.Require(role != null);

            if (this.Roles == null)
            {
                this.Roles = new List<Role>();
            }
            this.Roles.Add(role);
        }

        /// <summary>
        /// 移除一个新角色
        /// </summary>
        /// <param name="role"></param>
        protected internal virtual void InternalRemoveRole(Role role)
        {
            Check.Require(role != null);

            if (this.Roles == null)
            {
                this.Roles = new List<Role>();
            }
            this.Roles.Remove(role);
        }


        /// <summary>
        /// 获取 / 设置 上次登录时间
        /// </summary>
        public virtual DateTime? LastLoginTime { get; set; }

        /// <summary>
        /// 获取 / 设置 本次登录时间
        /// </summary>
        public virtual DateTime? CurrentLoginTime { get; set; }

        /// <summary>
        /// 获取 / 设置 登录次数
        /// </summary>
        public virtual int LoginCount { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (string.IsNullOrEmpty(this.EmployeeId))
            {
                return this.FullName;
            }
            else
            {
                return string.Format("{0}({1})", this.FullName, this.EmployeeId);
            }
        }
    }
}
